package br.com.ia.struts.action;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.enovar.trex.core.model.BaseModel;
import br.com.enovar.trex.struts.action.TRexCRUDBaseAction;
import br.com.enovar.trex.struts.form.TRexBaseForm;
import br.com.ia.business.VariavelBusiness;
import br.com.ia.model.Valor;
import br.com.ia.model.Variavel;
import br.com.ia.struts.form.VariavelForm;

public class VariavelAction extends TRexCRUDBaseAction{

	
	@Override
	public ActionForward insert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		VariavelForm variavelForm = (VariavelForm)form;
		String variavelDescricao = variavelForm.getDescricao();
		String valorDescricao = variavelForm.getValor().getDescricao();
		String flagNovaVariavel = variavelForm.getNovaVariavel();
		Integer codigoVariavel = variavelForm.getCodigoAux();
		
		Variavel variavel = new Variavel();
		variavel.setDescricao(variavelDescricao);
		
		Valor valor = new Valor();
		valor.setDescricao(valorDescricao);
		
		VariavelBusiness business = (VariavelBusiness)getBusinessClass(VariavelBusiness.class);
		if("S".equals(flagNovaVariavel)){
			business.insertVariavel(variavel, valor);
		}else{
			variavel.setCodigo(codigoVariavel);
			business.insertValor(variavel, valor);
		}
		
		
		return search(mapping, variavelForm, request, response);
	}
	
	public ActionForward setObjetivo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		VariavelForm variavelForm = (VariavelForm)form;
		Integer codigo = variavelForm.getCodigo();
		
		VariavelBusiness business = (VariavelBusiness)getBusinessClass(VariavelBusiness.class);
		business.updateFlagObjetivo(codigo);
		
		return search(mapping, form, request, response);
	}
	
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		VariavelForm variavelForm = (VariavelForm) form;
		VariavelBusiness business = (VariavelBusiness) getBusinessClass(VariavelBusiness.class);

		List<Variavel> list = business.buscarVariavel(variavelForm
				.getDescricao());

		request.setAttribute("list", list);
		return mapping.findForward("search");

	}

	@Override
	public BaseModel copyProperties(TRexBaseForm arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void copyProperties(BaseModel arg0, TRexBaseForm arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Serializable getId(TRexBaseForm arg0, HttpServletRequest arg1) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
